APM.BusinessDetailExperienceDefaultController = Ember.Controller.extend({
	needs: ["businessCenter", "businessDetail"],

	business: Ember.computed.alias('controllers.businessDetail.model'),

	timePoint: Ember.computed.oneWay('controllers.businessDetail.timePoint'),

	businessChange: Ember.observer("business", function(){
		this.requirePerformances();
	}),

	init: function(){
		this.set("cancelSet",true);

	},

	updatePerformances: function(){
		var perfs = this.get("business.performances");
		if( !(perfs && perfs.length > 0) ){
			return;
		}



		this.requirePerformances(perfs[0].timestamp);
	},

	positionShow:false,

	panelTitle: Ember.computed(function(){
		if($.cookie("isThreshold") == "business"){
			return "交易量";
		}else{
			return "吞吐量";
		}
	}),

	appApdexAtTimePoint: Ember.computed("business.performances", "timePoint", function(key, value){

		var performance = this._getPerformance();
		if(overviewOrCenterSwitch == "business"){
			var apdex = performance && performance["business/score/apdex"];
		}else{
			var apdex = performance && performance["system/score/apdex"];
		}
		return apdex === undefined ? "-" : apdex + "  [" + this.get("business").apdex_t + "]";
	}),

	appResponseTimeAtTimePoint: Ember.computed("business.performances", "timePoint", function(key, value){
		var performance = this._getPerformance();
		if(overviewOrCenterSwitch == "business"){
			var responseTime = performance && performance["business/response_time/avg"];
		}else{
			var responseTime = performance && performance["system/response_time/avg"];
		}
		
		return APM.util.indicatorFormatter("transform_time_ms")(responseTime);
	}),

	appRequestAtTimePoint: Ember.computed("business.performances", "timePoint", function(key, value){
		var performance = this._getPerformance();
		if(overviewOrCenterSwitch == "business"){
			var request = performance && performance["business/throughput/total"];
		}else{
			var request = performance && performance["system/throughput/total"];
		}
		
		return request === undefined ? "-" : request;
	}),

	appResponseUniqueAtTimePoint:Ember.computed("business.performances", "timePoint", function(key, value){
		var performance = this._getPerformance();
		if(overviewOrCenterSwitch == "business"){
			var request = performance && performance["business/user/unique"];
		}else{
			var request = performance && performance["system/user/unique"];
		}
		return request === undefined ? "-" : request;
	}),

	appResponseRateAtTimePoint:Ember.computed("business.performances", "timePoint", function(key, value){
		var performance = this._getPerformance();
		if(overviewOrCenterSwitch == "business"){
			var request = performance && performance["business/success/rate"];
		}else{
			var request = performance && performance["system/success/rate"];
		}
		return request === undefined ? "-" : request;
	}),

	_getPerformance: function(){
		var performances = Ember.A(this.get("business.performances")||[]);
		var timePoint = this.get("timePoint");
		var performance;
		if(!timePoint){
			if(performances.length>0){
				performance = performances[performances.length-1];
			}
		}else{
			performance = performances.findBy("timestamp", this.get("timePoint"));
		}
		return performance;
	},

	requirePerformances: function(time){
		$('#businessDetailView .apm-nav-tabs-left .nav-tabs').css({
			'padding-top':0
		});
		var ctrl = this;
		var business = this.get("business");
		if(!business)
			return;

		var startDate, endDate;
		if(time){
			startDate = new Date(time);
			endDate = new Date(+startDate)
			endDate.setHours(endDate.getHours()+1);
		}else{
			var business = this.get("business");
			if(business.performances){
				var perfs = business.performances;
				startDate = new Date(perfs[0].timestamp);
				endDate = new Date(+startDate);
				endDate.setHours(endDate.getHours()+1);
			}else{
				endDate = new Date(new Date() - timeD_value);
				endDate.setMinutes(endDate.getMinutes()-1);
				endDate.setSeconds(0);
				endDate.setMilliseconds(0);
				startDate = new Date(+endDate);
				startDate.setHours(startDate.getHours()-1);
			}
		}

		var params1 = {
			appId: business.id,
			sortField: 'system/response_time/avg',
			startDate: +startDate,
			endDate: +endDate
		};
		var params2 = {
			appId: business.id,
			sortField: 'system/throughput/total',
			startDate: +startDate,
			endDate: +endDate
		};
		var params3 = {
			appId: business.id,
			sortField: 'system/error/count',
			startDate: +startDate,
			endDate: +endDate
		};

		//开发调试模式参数
		if( isLocalDebugModel ){
			params1 = params2 = params3 = {
				appId: business.id,
			};
		}
		//响应时间地理位置
		this.store.ajax("GET", "performances/area", params1)
		.then(function(response){
			ctrl.set("rtAreaData", Ember.A(response["result"]));
			ctrl.set("mapType", response["mapType"]);
			return response;
		});
		//吞吐量地理位置
		this.store.ajax("GET", "performances/area", params2)
		.then(function(response){
			ctrl.set("rAreaData", Ember.A(response["result"]));
			ctrl.set("mapType", response["mapType"]);
			return response;
		});
		//错误数地理位置
		this.store.ajax("GET", "performances/area", params3)
		.then(function(response){
			ctrl.set("eAreaData", Ember.A(response["result"]));
			ctrl.set("mapType", response["mapType"]);
			return response;
		});
	},

	/*
		启动实时模式
	*/
	startRealTimeMode: function(){
		this.addObserver("business.performances", this, "updatePerformances");
	},

	/*
		停止动实时模式
	*/
	stopRealTimeMode: function(){
		this.removeObserver("business.performances", this, "updatePerformances");
	}
});

	